package com.yao.spider.yiyao;

import okhttp3.*;

import java.io.IOException;
import java.util.HashMap;

public class HttpUtil {
    public static final String token_name = "X-XSRF-TOKEN";
    public static final String cookie_name = "Cookie";

    public static void firstPage() throws IOException {

        String url = "http://10.58.33.42/#/login";
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url)
                .build();
        final Call call = okHttpClient.newCall(request);
        Response response = call.execute();
        System.out.println(response.toString());
    }

    public static void token() {
        String url = "http://10.58.33.42/ips/auth/random/token";

    }

    public static void post() throws Exception {
        final OkHttpClient client = new OkHttpClient();

        Request request = new Request.Builder()
                .headers(Headers.of(buildHeaderWithCookie("7546ae37-e683-4e25-86fb-c3c943a50849", "MjFjMzdlNWYtMmExZi00NWNmLWEwMTgtYzMwZTFmZjAxNTNh; SERVERID=28c61a4e67d724f94f4dd0b5c6012236|1650421386|1650357383")))
                .url("http://10.58.33.42/mbs/web/commonquery/getPsnByCondition")
                .post(buildBody())
                .build();

        Response response = client.newCall(request).execute();
        if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
        System.out.println(response.body().string());
    }

    public static RequestBody buildBody() {
        RequestBody formBody = new FormBody.Builder()
                .add("certno", "32132*********7856")
                .add("psnCertType", "01")
                .add("searchInsuEmp", "true")
                .add("_modulePartId_", "")
                .add("frontUrl", "http://10.58.33.42/mbs-ui/nation/N1412Local.html#/N141201Local")
                .build();
        return formBody;
    }
    public static Headers buildHeaders() {
        HashMap headersMap = new HashMap();
        headersMap.put("Host", "10.58.33.42");
        headersMap.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0");
        headersMap.put("Accept", "application/json, text/plain, */*");
        headersMap.put("Accept-Language", "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2");
        headersMap.put("Accept-Encoding", "gzip, deflate");
        headersMap.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
//        headersMap.put("X-XSRF-TOKEN", "e2be7aba-6e99-4b73-9d11-b5e61d050130");
        headersMap.put("Content-Length", "162");
        headersMap.put("Origin", "http://10.58.33.42");
        headersMap.put("Connection", "keep-alive");
        headersMap.put("Referer", "http://10.58.33.42/mbs-ui/nation/N1412Local.html");
//        headersMap.put("Cookie", "XSRF-TOKEN=e2be7aba-6e99-4b73-9d11-b5e61d050130; SESSION=NjQwMjc1M2EtNzQzZS00ZDZmLWFjOGUtNTc5YmRjZWQzNmIx; SERVERID=c69c01a6b756a27217e1f6bd4e7485e0|1648199241|1648199219");
        return Headers.of(headersMap);
    }
    public static HashMap<String, String> buildHeaderMap() {
        HashMap headersMap = new HashMap();
        headersMap.put("Host", "10.58.33.42");
        headersMap.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0");
        headersMap.put("Accept", "application/json, text/plain, */*");
        headersMap.put("Accept-Language", "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2");
        headersMap.put("Accept-Encoding", "gzip, deflate");
        headersMap.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
//        headersMap.put("X-XSRF-TOKEN", "e2be7aba-6e99-4b73-9d11-b5e61d050130");
        headersMap.put("Content-Length", "162");
        headersMap.put("Origin", "http://10.58.33.42");
        headersMap.put("Connection", "keep-alive");
        headersMap.put("Referer", "http://10.58.33.42/mbs-ui/nation/N1412Local.html");
//        headersMap.put("Cookie", "XSRF-TOKEN=e2be7aba-6e99-4b73-9d11-b5e61d050130; SESSION=NjQwMjc1M2EtNzQzZS00ZDZmLWFjOGUtNTc5YmRjZWQzNmIx; SERVERID=c69c01a6b756a27217e1f6bd4e7485e0|1648199241|1648199219");
//        HashMap<String, String> cokkie = buildHeaderWithCookie("7546ae37-e683-4e25-86fb-c3c943a50849", "MjFjMzdlNWYtMmExZi00NWNmLWEwMTgtYzMwZTFmZjAxNTNh; SERVERID=28c61a4e67d724f94f4dd0b5c6012236|1650421386|1650357383");
//        headersMap.putAll(cokkie);
        return headersMap;
    }
    public static HashMap<String, String> buildYpExplanationHeaderMap() {
        HashMap headersMap = new HashMap();
        headersMap.put("Host", "www.yaopinnet.com");
        headersMap.put("Accept", "*/*");
        headersMap.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/109.0");
        headersMap.put("Accept-Language", "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2");
        headersMap.put("Accept-Encoding", "gzip, deflate, br");
        headersMap.put("X-Requested-With", "XMLHttpRequest");
        headersMap.put("Connection", "keep-alive");
        headersMap.put("Referer", "https://www.yaopinnet.com/tools/sms.asp");
        headersMap.put("Cookie", "ASPSESSIONIDSEBCRRSR=NHPHPJEDNOPDONKHJMLOFDAA");
        headersMap.put("Sec-Fetch-Dest", "empty");
        headersMap.put("Sec-Fetch-Mode", "cors");
        headersMap.put("Sec-Fetch-Site", "same-origin");
        return headersMap;
    }
    public static HashMap<String, String> buildYaoZhHeaderMap() {
        HashMap headersMap = new HashMap();
        headersMap.put("Host", "db.yaozh.com");
        headersMap.put("Accept", "application/json, text/javascript, */*; q=0.01");
        headersMap.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/109.0");
        headersMap.put("Accept-Language", "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2");
//        headersMap.put("Accept-Encoding", "gzip, deflate, br");
        headersMap.put("X-Requested-With", "XMLHttpRequest");
        headersMap.put("Connection", "keep-alive");
        headersMap.put("Referer", "https://www.yaozh.com/");
        headersMap.put("Cookie",
                "Path=/; kztoken=nJail6zJp6iXaJqWmHBoZGNvY5ab; his=a%3A10%3A%7Bi%3A0%3Bs%3A28%3A%22nJail6zJp6iXaJqWmHBoZGNtaZSZ%22%3Bi%3A1%3Bs%3A28%3A%22nJail6zJp6iXaJqWmHBoZGNtaZSb%22%3Bi%3A2%3Bs%3A28%3A%22nJail6zJp6iXaJqWmHBoZGNtaZWT%22%3Bi%3A3%3Bs%3A28%3A%22nJail6zJp6iXaJqWmHBoZGNtaZWZ%22%3Bi%3A4%3Bs%3A28%3A%22nJail6zJp6iXaJqWmHBoZGNuYpmX%22%3Bi%3A5%3Bs%3A28%3A%22nJail6zJp6iXaJqWmHBoZGNuYpqW%22%3Bi%3A6%3Bs%3A28%3A%22nJail6zJp6iXaJqWmHBoZGNvYZiW%22%3Bi%3A7%3Bs%3A28%3A%22nJail6zJp6iXaJqWmHBoZGNvYZuS%22%3Bi%3A8%3Bs%3A28%3A%22nJail6zJp6iXaJqWmHBoZGNvYZyU%22%3Bi%3A9%3Bs%3A28%3A%22nJail6zJp6iXaJqWmHBoZGNvY5ab%22%3B%7D; _pk_id.1.c32b=f4e4408a461d3934.1673494483.; ipnum=127.0.0.1.741; ipci=%C3%A6%C5%93%C2%AA%C3%A7%C5%B8%C2%A5; yaozh_uidhas=1; _ga=GA1.3.615524423.1673494483; yaozh_mylogin=1675242650; PHPSESSID=j0bbvfab5iojtuiq46r34em3i7; Path=/; _pk_ref.1.c32b=%5B%22%22%2C%22%22%2C1676423243%2C%22https%3A%2F%2Fwww.yaozh.com%2F%22%5D; _pk_ses.1.c32b=1; Hm_lvt_19d20990a54a49c29ef6894ac385ed90=1675242645,1675836214,1675934407,1676423243; _gid=GA1.2.52618063.1676423243; _clck=8emt9m|1|f95|0; Hm_lvt_65968db3ac154c3089d7f9a4cbb98c94=1675074609,1675934433,1676423256; Hm_lpvt_65968db3ac154c3089d7f9a4cbb98c94=1676423256; _ga=GA1.1.615524423.1673494483; yaozh_logintime=1676423258; yaozh_user=1333591%09sssssssy; yaozh_userId=1333591; yaozh_jobstatus=kptta67UcJieW6zKnFSe2JyYnoaSaphomp2Wg26qb21rg66flM6bh5%2BscZlyVNbY1dTX19Swh3DYnp6bVaOWq6XArZqgxlig13NokXJUlJu98c7e5E8da73cff0b8Ed52adCd188f26Zlp2XlWuYaoef2JtncVesms6eU27UcJaXc1mSbmmZmZSUlpmabplqh5%2Fi98f3b3b075e7d016a172dfb9ac8c56ac; db_w_auth=1062111%09sssssssy; _gid=GA1.3.52618063.1676423243; acw_tc=76b20f4816764256066687651e309b37e56980abe32b4745d2ced91625444b; expire=1676443445394; think_language=zh-CN; _currentUrl_=%2FIndex%2FgetRecSupply%3Fcallback%3DjQuery11240415238138290821_1676427089083%26search%3DURLEncoder.encode%28%26_%3D1676427089084; _clsk=zr31vo|1676427093696|23|1|j.clarity.ms/collect; kztoken=nJail6zJp6iXaJqWmHBoZGNvY5eV; his=a%3A10%3A%7Bi%3A0%3Bs%3A28%3A%22nJail6zJp6iXaJqWmHBoZGNrY5mU%22%3Bi%3A1%3Bs%3A28%3A%22nJail6zJp6iXaJqWmHBoZGNrY5qT%22%3Bi%3A2%3Bs%3A28%3A%22nJail6zJp6iXaJqWmHBoZGNrY5qX%22%3Bi%3A3%3Bs%3A28%3A%22nJail6zJp6iXaJqWmHBoZGNrY5qb%22%3Bi%3A4%3Bs%3A28%3A%22nJail6zJp6iXaJqWmHBoZGNrapiV%22%3Bi%3A5%3Bs%3A28%3A%22nJail6zJp6iXaJqWmHBoZGNtZ5Sa%22%3Bi%3A6%3Bs%3A28%3A%22nJail6zJp6iXaJqWmHBoZGNtaZWY%22%3Bi%3A7%3Bs%3A28%3A%22nJail6zJp6iXaJqWmHBoZGNvYZiV%22%3Bi%3A8%3Bs%3A28%3A%22nJail6zJp6iXaJqWmHBoZGNvYZuW%22%3Bi%3A9%3Bs%3A28%3A%22nJail6zJp6iXaJqWmHBoZGNvY5eV%22%3B%7D; Hm_lpvt_19d20990a54a49c29ef6894ac385ed90=1676427248; _gat=1; _ga_W6DF8W4GMP=GS1.1.1676423243.21.1.1676427248.0.0.0"
                );

        headersMap.put("Sec-Fetch-Dest", "empty");
        headersMap.put("Sec-Fetch-Mode", "cors");
        headersMap.put("Sec-Fetch-Site", "same-origin");
        return headersMap;
    }
    public static HashMap<String, String> buildDongFHeaderMap() {
        HashMap headersMap = new HashMap();
        headersMap.put("Host", "www.yaozs.com");
        headersMap.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
        headersMap.put("Accept-Language", "zh-CN,zh;q=0.9");
        headersMap.put("Connection", "keep-alive");
        headersMap.put("Cookie", "Hm_lvt_4b4d2998331ccb9a1abe9eae8d46e242=1675221253;");
        headersMap.put("Referer", "https://www.yaozs.com/data/sms-htm-page-1.html");
        headersMap.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36");
//        headersMap.put("Accept-Encoding", "gzip, deflate, br");

        headersMap.put("sec-ch-ua", "\"Not?A_Brand\";v=\"8\", \"Chromium\";v=\"108\", \"Google Chrome\";v=\"108\"");
        headersMap.put("sec-ch-ua-mobile", "?0");
        headersMap.put("sec-ch-ua-platform", "Windows");
        headersMap.put("sec-ch-ua-mobile", "?0");
        headersMap.put("sec-ch-ua-mobile", "?0");

        headersMap.put("Sec-Fetch-Dest", "document");
        headersMap.put("Sec-Fetch-Mode", "navigate");
        headersMap.put("Sec-Fetch-Site", "same-origin");
        headersMap.put("Sec-Fetch-User", "?1");
        headersMap.put("Upgrade-Insecure-Requests", "1");
        return headersMap;
    }



    public static HashMap<String, String> buildHeaderWithCookie(String token, String session) {
        HashMap<String, String> headerMap = HttpUtil.buildHeaderMap();
        headerMap.put(token_name, token);
        headerMap.put(cookie_name, "XSRF-TOKEN=" + token + "; " + "SESSION=" + session);
        return headerMap;
    }

    //查看详情
    public static void getPsnBaseInfo() throws IOException {
        final OkHttpClient client = new OkHttpClient();
        RequestBody formBody = new FormBody.Builder()
                .add("psnNo", "321300000000000010025*****")
                .add("_modulePartId_", "")
                .add("frontUrl", "http://10.58.33.42/mbs-ui/nation/N1412Local.html#/N141201Local")
                .build();
        Request request = new Request.Builder()
                .headers(buildHeaders())
                .url("http://10.58.33.42/mbs/web/psnCommqueryLocal/getPsnBaseInfo")
                .post(formBody)
                .build();

        Response response = client.newCall(request).execute();
        if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
        System.out.println(response.body().string());
    }
    //人员就诊信息
    public static void queryMdtrtDInfoOder() throws IOException {
        final OkHttpClient client = new OkHttpClient();
        RequestBody formBody = new FormBody.Builder()
                .add("psnNo", "321300000000000010025*****")
                .add("_modulePartId_", "")
                .add("frontUrl", "http://10.58.33.42/mbs-ui/nation/N1412Local.html#/N141201Local")
                .build();
        Request request = new Request.Builder()
                .headers(buildHeaders())
                .url(" http://10.58.33.42/mbs/web/multisearch/mdtrtD/queryMdtrtDInfoOder")
                .post(formBody)
                .build();

        Response response = client.newCall(request).execute();
        if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
        System.out.println(response.body().string());
    }
    //药店购药信息
    public static void queryPsnSetlInfo() throws IOException {
        final OkHttpClient client = new OkHttpClient();
        RequestBody formBody = new FormBody.Builder()
                .add("psnNo", "321300000000000010025*****")
                .add("pageNum", "1")
                .add("pageSize", "10")
                .add("str", "10")
                .add("_modulePartId_", "")
                .add("frontUrl", "http://10.58.33.42/mbs-ui/nation/N1412Local.html#/N141201Local")
                .build();
        Request request = new Request.Builder()
                .headers(buildHeaders())
                .url("http://10.58.33.42/mbs/web/treatmentQueryLocal/queryPsnSetlInfo")
                .post(formBody)
                .build();

        Response response = client.newCall(request).execute();
        if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
        System.out.println(response.body().string());
    }

  //药店购药信息--费用明细  和购药明细一样
    public static void feeListQueryByPage11() throws IOException {
        final OkHttpClient client = new OkHttpClient();
        RequestBody formBody = new FormBody.Builder()
                .add("psnNo", "321300000000000010025*****")
                .add("pageNum", "1")
                .add("pageSize", "10")
                .add("setlId", "SQ112280396117")
                .add("mdtrtId", "SQ112280396115")
                .add("_modulePartId_", "")
                .add("frontUrl", "http://10.58.33.42/mbs-ui/nation/N1412Local.html#/N141201Local")
                .build();
        Request request = new Request.Builder()
                .headers(buildHeaders())
                .url("http://10.58.33.42/mbs/web/feeListInfoLocal/feeListQueryByPage")
                .post(formBody)
                .build();

        Response response = client.newCall(request).execute();
        if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
        System.out.println(response.body().string());
    }



    //个人门诊信息
    public static void queryPsnSetlInfo11() throws IOException {
        final OkHttpClient client = new OkHttpClient();
        RequestBody formBody = new FormBody.Builder()
                .add("psnNo", "321300000000000010025*****")
                .add("pageNum", "1")
                .add("pageSize", "10")
                .add("str", "11")
                .add("_modulePartId_", "")
                .add("frontUrl", "http://10.58.33.42/mbs-ui/nation/N1412Local.html#/N141201Local")
                .build();
        Request request = new Request.Builder()
                .headers(buildHeaders())
                .url("http://10.58.33.42/mbs/web/treatmentQueryLocal/queryPsnSetlInfo")
                .post(formBody)
                .build();

        Response response = client.newCall(request).execute();
        if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
        System.out.println(response.body().string());
    }

   //个人门诊信息----费用明细
    public static void feeListQueryByPage() throws IOException {
        final OkHttpClient client = new OkHttpClient();
        RequestBody formBody = new FormBody.Builder()
                .add("psnNo", "321300000000000010025*****")
                .add("pageNum", "1")
                .add("pageSize", "10")
                .add("str", "11")
                .add("_modulePartId_", "")
                .add("frontUrl", "http://10.58.33.42/mbs-ui/nation/N1412Local.html#/N141201Local")
                .build();
        Request request = new Request.Builder()
                .headers(buildHeaders())
                .url("http://10.58.33.42/mbs/web/treatmentQueryLocal/queryPsnSetlInfo")
                .post(formBody)
                .build();

        Response response = client.newCall(request).execute();
        if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
        System.out.println(response.body().string());
    }

    //个人住院信息
    public static void queryPsnSetlInfo12() throws IOException {
        final OkHttpClient client = new OkHttpClient();
        RequestBody formBody = new FormBody.Builder()
                .add("psnNo", "321300000000000010025*****")
                .add("pageNum", "1")
                .add("pageSize", "10")
                .add("str", "12")
                .add("_modulePartId_", "")
                .add("frontUrl", "http://10.58.33.42/mbs-ui/nation/N1412Local.html#/N141201Local")
                .build();
        Request request = new Request.Builder()
                .headers(buildHeaders())
                .url("http://10.58.33.42/mbs/web/treatmentQueryLocal/queryPsnSetlInfo")
                .post(formBody)
                .build();

        Response response = client.newCall(request).execute();
        if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
        System.out.println(response.body().string());
    }

    public static void main(String[] args) {
        try {
            firstPage();
//            queryPsnSetlInfo();
//            feeListQueryByPage();
//            queryMdtrtDInfoOder();
//            queryPsnSetlInfo11();
//            getPsnBaseInfo();
//            post();
//            HttpUtil.get();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
